/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter;


import net.cellobject.javacodeformatter.model.FormatterManager;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

/**
 * The activator class controls the plug-in life cycle
 */
public class FormatterPlugin extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "net.cellobject.javacodeformatter";

	// The shared instance
	private static FormatterPlugin plugin;
	

	// The configuration preferences
	private IEclipsePreferences configPrefs;
	
	/**
	 * The constructor
	 */
	public FormatterPlugin() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	@Override	
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	@Override
	public void stop(BundleContext context) throws Exception {
		FormatterManager.getManager().saveFavorites();
		FormatterManager.shutdown();
		saveConfigPrefs();
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static FormatterPlugin getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given
	 * plug-in relative path
	 *
	 * @param path the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}
	
	/**
	 * Answer the configuration preferences shared among multiple workspaces.
	 * 
	 * @return the configuration preferences or <code>null</code> if the configuration directory is read-only or unspecified.
	 */
	public Preferences getConfigPrefs() {
		if (configPrefs == null) {
			configPrefs = new ConfigurationScope().getNode(PLUGIN_ID);
		}
		return configPrefs;
	}

	/**
	 * Save the configuration preferences if they have been loaded
	 */
	public void saveConfigPrefs() {
		if (configPrefs != null) {
			try {
				configPrefs.flush();
			} catch (final BackingStoreException e) {
				e.printStackTrace();
			}
		}
	}
	
	public String getEclipseInstallLocation() {
		return Platform.getInstallLocation().getURL().getPath();
	}
	
}
